package com.itstherules.jmsunit.mock.activemq;

import static com.itstherules.jmsunit.JmsUnit.sender;
import static com.itstherules.jmsunit.JmsUnit.mock;
import static com.itstherules.jmsunit.expectation.Expectations.headers;
import static com.itstherules.jmsunit.expectation.XmlBasedExpectations.soapBodyXPathEq;
import static com.itstherules.jmsunit.expectation.XmlBasedExpectations.xPathEq;
import static com.itstherules.jmsunit.expectation.XmlLoaders.xml;

import org.junit.Before;
import org.junit.Test;

import com.itstherules.jmsunit.JmsInlineMock;
import com.itstherules.jmsunit.JmsSender;
import com.itstherules.jmsunit.managers.QueueManagersFactory.QueueManagerType;


public class JmsSoapXPathExpectationTest {
	
	private JmsInlineMock stub;
	private JmsSender sender;
	
	@Before
	public void create() throws Exception {
		stub = mock("vm://localhost:61616", QueueManagerType.ActiveMQ);
		sender = sender("blue", "vm://localhost:61616", QueueManagerType.ActiveMQ);
	}
	
	@Test
	public void xPathSoapEqualsFull() throws Exception {
		stub.expects()
			.through("blue")
			.with(soapBodyXPathEq("a", "/root/a"))
			.with(soapBodyXPathEq("b", "/root/b"))
			.with(soapBodyXPathEq("e", "/root/c/e"));
		
		sender.send(xml("simpleSoapMessage"), headers("Blue2"));		
		stub.verifyAndClose();
	}
	
	@Test
	public void xPathSoapEqualsFullOtherPrefix() throws Exception {
		stub.expects()
			.through("blue")
			.with(xPathEq("3", 
					"count(/*[local-name()='Envelope' and namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']/*[local-name()='Body' and namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']" +
					"/root/*" + 
					")"))	
			.with(soapBodyXPathEq("a", "/root/a"))
			.with(soapBodyXPathEq("b", "/root/b"))
			.with(xPathEq("3", 
					"count(/*[local-name()='Envelope' and namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']/*[local-name()='Body' and namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']" +
					"/root/c/*" + 
					")"))
			.with(soapBodyXPathEq("e", "/root/c/e"));
		
		sender.send(xml("simpleSoapMessageDifferentPrefix"), headers("Blue2"));		
		stub.verifyAndClose();
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void xPathSoapEqualsFullUnsupportedUseOfFunctionCall() throws Exception {
		stub.expects()
			.through("blue")
			.with(soapBodyXPathEq("3", "count(/root/*)"));
			
		sender.send(xml("simpleSoapMessage"), headers("Blue2"));		
		stub.verifyAndClose();
	}
	
	@Test
	public void xPathSoapEqualsFullSupportedUseOfFunctionCall() throws Exception {
		stub.expects()
			.through("blue")
			.with(xPathEq("3", 
					"count(/*[local-name()='Envelope' and namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']/*[local-name()='Body' and namespace-uri()='http://schemas.xmlsoap.org/soap/envelope/']" +
					"/root/*" + 
					")"));
			
		sender.send(xml("simpleSoapMessage"), headers("Blue2"));		
		stub.verifyAndClose();
	}
	
	
}
